/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.cells;

import cn.ac.nercis.pes.common.constant.common.BaseConstants;
import cn.ac.nercis.pes.community.utils.ControlUtils;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 * 自动换行单元格
 * @author zpy
 * @version 1.0.0
 */
public class AutoWrapTableCell<S, T> extends TableCell<S, T> {
    private final Map<String,String> TOOLTIP_STRATEGY;
    private final String cellStyleCss;
    private final Boolean enableCopy;

    public AutoWrapTableCell(){
        this(new HashMap<>(),"");
    }

    public AutoWrapTableCell(boolean enableCopy){
        this(new HashMap<>(),"",enableCopy);
    }
    public AutoWrapTableCell(String cellStyleCss){
        this(new HashMap<>(),cellStyleCss);
    }

    public AutoWrapTableCell(Map<String,String> tooltips){
        this(tooltips,"");
    }


    public AutoWrapTableCell(Map<String,String> tooltips,String cellStyleCss){
        this(tooltips,cellStyleCss,false);
    }

    public AutoWrapTableCell(Map<String,String> tooltips,String cellStyleCss,Boolean enableCopy){
        TOOLTIP_STRATEGY = tooltips;
        this.cellStyleCss = cellStyleCss;
        this.enableCopy = enableCopy;
        if(StringUtils.isNotBlank(cellStyleCss)){
            getStyleClass().add(cellStyleCss);
        }
    }

    @Override
    protected void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            String content = getValue() == null ? "" : String.valueOf(getValue());
            VBox vBox = new VBox();
            vBox.getStyleClass().add(StringUtils.isBlank(cellStyleCss)?"v-box":cellStyleCss);
            Stream.of(content.split(BaseConstants.REGEX_WRAP_TEXT_SPLIT))
                    .filter(StringUtils::isNotBlank)
                    .forEach(value->{
                        Label label = new Label(value);
                        label.setWrapText(true);
                        if(StringUtils.isNotBlank(value)){
                            label.setTooltip(new Tooltip(TOOLTIP_STRATEGY.getOrDefault(value,value)));
                        }
                        vBox.getChildren().add(label);
                    });
            setGraphic(vBox);
            if(enableCopy){
                ControlUtils.appendCopy(vBox,null);
            }
        }
    }

    protected Object getValue() {
        return getItem();
    }
}